package swu.edu.cn.javaee.service;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import swu.edu.cn.javaee.entity.Friend;
import swu.edu.cn.javaee.mapper.FriendMapper;
import swu.edu.cn.javaee.mapper.UserMapper;

import java.lang.reflect.Field;
import java.util.List;

import static swu.edu.cn.javaee.component.Signal.*;

@Service
public class FriendService {
    @Resource
    private FriendMapper friendMapper;
    @Resource
    private UserMapper userMapper;

    /**
     * 查询所有用户好友关系
     * return list
     * */
    public List<Friend> queryAllFriend(String userId){
        List<Friend> list=friendMapper.queryFriendList(userId);
        if(list.size()!=0){
            System.out.println(list);
            return list;
        }
        return null;
    }

    /**
     * 根据好友分组类别查询好友
     * */
    public List<Friend> queryFriendByUserGroup(String userGroup){
        List<Friend> list=friendMapper.queryByUserGroup(userGroup);
        if(list.size()!=0){
            System.out.println(list);
            return list;
        }
        return null;
    }

    /**
     * 新增好友关系
     * */
    public String saveFriend(Friend friend){
        Friend friend1=friendMapper.queryFriend(friend);
        if(friend1==null){
            friendMapper.saveFriend(friend);
            return SUCCESS_CODE;
        }
        System.out.println(FAILED_CODE_FRIEND_EXIST);
        return FAILED_CODE_FRIEND_EXIST;
    }
    /**
     * 修改好友分组
     * */
    public String updateFriend(Friend friend){
        Friend friend1=friendMapper.queryFriend(friend);
        if(friend1!=null){
            friend1.setUserGroup(friend.getUserGroup());
            friendMapper.updateFriend(friend1);
            return SUCCESS_CODE;
        }
        return FAILED_CODE_FRIEND_NO_EXIST;
    }


    /**
     * 删除好友功能
     * @param friend
     * @return
     */
    public String deleteFriend(Friend friend){
        Friend friend1=friendMapper.queryFriend(friend);
        if(friend1!=null){
            friendMapper.deleteFriend(friend);
            return SUCCESS_CODE;
        }
        return FAILED_CODE_FRIEND_NO_EXIST;
    }

}
